extends Node
class_name TimeSystem

signal time_changed(day: int, hour: int)
signal day_changed(day: int)

var current_day := 1
var current_hour := 8
var time_speed := 1
var is_paused := false

var time_accumulator := 0.0
const HOUR_DURATION := 60.0  # 1小时 = 60秒实际时间

func _ready():
	set_process(true)

func _process(delta):
	if is_paused:
		return
	
	time_accumulator += delta * time_speed
	
	if time_accumulator >= HOUR_DURATION:
		time_accumulator -= HOUR_DURATION
		advance_hour()

func advance_hour():
	current_hour += 1
	if current_hour >= 24:
		current_hour = 0
		current_day += 1
		day_changed.emit(current_day)
	
	time_changed.emit(current_day, current_hour)

func set_time(day: int, hour: int):
	current_day = day
	current_hour = hour
	time_changed.emit(current_day, current_hour)

func set_speed(speed: int):
	time_speed = speed

func pause():
	is_paused = true

func resume():
	is_paused = false

func get_time_string() -> String:
	return "第%d天 %02d:00" % [current_day, current_hour]

func is_day_time() -> bool:
	return current_hour >= 6 and current_hour < 20

func get_game_time_stamp() -> int:
	return (current_day - 1) * 24 + current_hour